<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;
use Think\Model;
use Think\Db;
use OT\Database;

class SysController extends AuthController {

    //站点设置
    public function sys()
    {
        if (!IS_AJAX)
        {
    	   $sys=M('sys')->where(array('sys_id'=>1))->find();
    	   $this->assign('sys',$sys)->display();
    	}
        else
        {
    		M('sys')->save($_POST);
    		$this->success('设置保存成功',1,1);
    	}

    }
    

   
    //发送邮件设置
    public function emailsys()
    {
        if (!IS_AJAX)
        {
            $sys=M('sys')->where(array('sys_id'=>1))->find();
            $this->assign('sys',$sys)->display();
    	}
        else
        {
    		M('sys')->save($_POST);
    		$this->success('邮件设置保存成功',1,1);
    	}
    
    }

    //发送短信设置
    public function sms()
    {
        if (!IS_AJAX)
        {
            $sys=M('sys')->where(array('sys_id'=>1))->find();
            $this->assign('sys',$sys)->display();
        }
        else
        {    
            M('sys')->save($_POST);
            $this->success('短信设置保存成功',1,1);
        }
    
    }

    //公告管理
    public function notice()
    {
        $notice = D('notice');
        $count = $notice -> count();
        $p = getpage($count, C('PAGE_SIZE'));
        $show = $p -> show();
        $list = $notice -> page(I('get.p')) -> order('id desc') -> limit(C('PAGE_SIZE')) -> select();
        $this -> assign('page', $show);
        // 赋值分页输出
        $this -> assign('list', $list);
        $this -> display();
    
    }

    //增加系统公告
    public function addNotice()
    {   
        if (IS_POST) {
            $data = $this -> addCheck();
            $obj = D('notice');
            if ($obj -> add($data)) {
                $this -> success("保存成功", U('Sys/notice'));
                return;
            } else {
                $this -> error('操作失败！');
                return;
            }
        }

         $this->display();
    }

     private function addCheck() {
        $param = I('post.');
        $data = $this -> checkFields($param["data"], $this -> createnotice_fields);
        $add_time = strtotime(date("Y-m-d H:i:s", time()));
        $data['title'] = trim(I('post.title', '', 'htmlspecialchars'));
        if ( D('notice') -> getNoticeTitle($data['title'])) {
            $this -> error('消息已经存在');
            exit ;
        }
        if (empty($data['title'])) {
            $this -> error('消息标题不能为空');
        }
        $data['info'] = $_POST['info'];
        if (empty($data['info'])) {
            $this -> error('消息内容不能为空');
        }
        $data['add_time'] = $add_time;
        return $data;
    }
 

    public function editNotice($id = 0)
    {
         if ($id = (int)$id) {
            $obj = D('notice');
            if (!$detail = $obj -> find($id)) {
                $this -> error('请选择要编辑的系统消息');
            }
            if (IS_POST) {
                $data = $this -> editCheck();
                $data['id'] = $id;
                if ($obj -> save($data)) {
                    $this -> success('操作成功', U('Sys/notice'));
                } else {
                    $this -> error('操作失败');
                }
            } else {
                $this -> assign('detail', $detail);
                $this -> display();
            }
        } else {
            $this -> error('请选择要编辑的系统消息');
        }
    }


    private function editCheck() {
        $param = I('post.');
        $data = $this -> checkFields($param['data'], $this -> editnotice_fields);
        $update_time = strtotime(date("Y-m-d H:i:s", time()));
        $data['title'] = trim(I('post.title', '', 'htmlspecialchars'));
        if (empty($data['title'])) {
            $this -> error('消息标题不能为空');
        }
        $data['info'] = $_POST['info'];
        if (empty($data['info'])) {
            $this -> error('消息内容不能为空');
        }
        $data['update_time'] = $update_time;
        return $data;
    }

    public function delNotice() {
        $notice = D('notice');
        $id = I('get.id');
        $result = $notice -> where('id =' . $id . '') -> delete();
        if ($result) {
            $this -> success('删除成功', U('Sys/notice'));
        } else {
            $this -> error('删除失败');
        }
    }

    //删除缓存
    public function clearRuntime() 
    {          
        delFileByDir(APP_PATH.'Runtime/'); 
        $this->success('删除缓存成功！');
    }
    
    
/************************************管理员模块****************************************/
    
	public function admin_list(){

		$admin=M('admin');
		$val=I('val');
		$auth = new Auth();
		$this->assign('testval',$val);
		if($val){
			$map['admin_username']= array('like',"%".$val."%");
		}
		
		$count= $admin->where($map)->count();// 查询满足要求的总记录数
		$Page= new \Think\Page($count,C('DB_PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数

		foreach($map as $key=>$val) {
			$Page->parameter[$key]=urlencode($val);
		}

		$show= $Page->show();// 分页显示输出
		$admin_list=$admin->where($map)->order('admin_id')->limit($Page->firstRow.','.$Page->listRows)->select();

		foreach ($admin_list as $k=>$v){
			$group = $auth->getGroups($v['admin_id']);
			$admin_list[$k]['group'] = $group[0]['title'];
		}
		
		$this->assign('admin_list',$admin_list);
		$this->assign('page',$show);		
		$this->display();
	}
    




    public function admin_list_add(){
        
        if (!IS_AJAX){
    		$auth_group=M('auth_group')->select();
    		$this->assign('auth_group',$auth_group);
    		$this->display();
        }else{
            $admin=M('admin');
    		$admin_access=M('auth_group_access');
    		$check_user=$admin->where(array('admin_username'=>I('admin_username')))->find();
    		if ($check_user){
    			$this->error('用户已存在，请重新输入用户名',0,0);
    		}
    		$sldata=array(
    			'admin_username'=>I('admin_username'),
    			'admin_pwd'=>I('admin_pwd','','md5'),
    			'admin_email'=>I('admin_email'),
                'admin_img'=>I('admin_img'),
    			'admin_tel'=>I('admin_tel'),
    			'admin_open'=>I('admin_open'),
    			'admin_realname'=>I('admin_realname'),
    			'admin_ip'=>get_client_ip(),
    			'admin_addtime'=>time(),
    		);
    		$result=$admin->add($sldata);
    		$accdata=array(
    			'uid'=>$result,
    			'group_id'=>I('group_id'),
    		);
    		$admin_access->add($accdata);
    		$this->success('用户添加成功',U('admin_list'),1);
        }  
		
	}

	

	public function admin_list_edit(){
            
        if (!IS_AJAX){
        	$auth_group=M('auth_group')->select();
    		$admin_list=M('admin')->where(array('admin_id'=>I('admin_id')))->find();
    		$auth_group_access=M('auth_group_access')->where(array('uid'=>$admin_list['admin_id']))->getField('group_id');
    		$this->assign('admin_list',$admin_list);
    		$this->assign('auth_group',$auth_group);
    		$this->assign('auth_group_access',$auth_group_access);
    		$this->display();
        }else{
            $admin_list=M('admin');
            $admin_pwd=I('admin_pwd');
            $group_id=I('group_id');
            $admindata['admin_id']=I('admin_id');
            if ($admin_pwd){
                $admindata['admin_pwd']=I('admin_pwd','','md5');
            }
            $admindata['admin_email']=I('admin_email');
            $admindata['admin_img']=I('admin_img');
            $admindata['admin_tel']=I('admin_tel');
            $admindata['admin_realname']=I('admin_realname');
            $admindata['admin_open']=I('admin_open');
            $admin_list->save($admindata);
            //修改用户组
            M('auth_group_access')->where(array('uid'=>I('admin_id')))->setField('group_id',$group_id);
            $this->success('用户修改成功',U('admin_list'),1);
        }
            		
	}
	
	

    public function admin_list_del()
    {

        $admin_id=I('admin_id');
    	if (empty($admin_id))
        {
    		$this->error('用户ID不存在',U('admin_list'),1);
    	}
    	M('admin')->where(array('admin_id'=>I('admin_id')))->delete();
    	M('auth_group_access')->where(array('uid'=>I('admin_id')))->delete();
    	$this->redirect('admin_list');
    	
    }
    


    public function admin_list_state()
    {
    	$id=I('x');
    	if (empty($id))
        {
    		$this->error('用户ID不存在',U('admin_list'),1);
    	}
    	$status=M('admin')->where(array('admin_id'=>$id))->getField('admin_open');//判断当前状态情况
    	if($status==1)
        {
    		$statedata = array('admin_open'=>0);
    		$auth_group=M('admin')->where(array('admin_id'=>$id))->setField($statedata);
    		$this->success('状态禁止',1,1);
    	}
        else
        {
    		$statedata = array('admin_open'=>1);
    		$auth_group=M('admin')->where(array('admin_id'=>$id))->setField($statedata);
    		$this->success('状态开启',1,1);
    	}
    
    }
    


    //用户组管理
    public function admin_group()
    {
    	$auth_group=M('auth_group')->select();
    	$this->assign('auth_group',$auth_group);
    	$this->display();
    }


    
    public function admin_group_add()
    {

    	if (!IS_AJAX)
        {
    		$this->display();
    	}
        else
        {
    		$sldata=array(
    			'title'=>I('title'),
    			'status'=>I('status'),
    			'addtime'=>time(),
    		);
    		M('auth_group')->add($sldata);
    		$this->success('角色添加成功',U('admin_group'),1);
    	}
    }
    


    public function admin_group_del()
    {
    	M('auth_group')->where(array('id'=>I('id')))->delete();
    	$this->redirect('admin_group');           
    }



    
    public function admin_group_edit()
    {  
        
        if (!IS_AJAX)
        {
            $group=M('auth_group')->where(array('id'=>I('id')))->find();
            $this->assign('group',$group);
            $this->display();      
    	}
        else
        {
    		$data=array(
				'id'=>I('id'),
				'title'=>I('title'),
				'status'=>I('status'),
    		);
    		M('auth_group')->save($data);
    		$this->success('角色修改成功',U('admin_group'),1);
    	}
    }

  
    
    public function admin_group_state()
    {

    	$id=I('x');
    	$status=M('auth_group')->where(array('id'=>$id))->getField('status');//判断当前状态情况
    	if($status==1)
        {
    		$statedata = array('status'=>0);
    		$auth_group=M('auth_group')->where(array('id'=>$id))->setField($statedata);
			$this->success('状态禁止',1,1);
    	}
        else
        {
    		$statedata = array('status'=>1);
    		$auth_group=M('auth_group')->where(array('id'=>$id))->setField($statedata);
    		$this->success('状态开启',1,1);
    	}
    }

     //前台用户组管理
    public function home_group()
    {
        $auth_group_home=M('auth_group_home')->select();
        $this->assign('auth_group',$auth_group_home);
        $this->display();
    }


    
    public function home_group_add()
    {

        if (!IS_AJAX)
        {
            $this->display();
        }
        else
        {
            $sldata=array(
                'title'=>I('title'),
                'status'=>I('status'),
                'addtime'=>time(),
            );
            M('auth_group_home')->add($sldata);
            $this->success('角色添加成功',U('home_group'),1);
        }
    }
    


    public function home_group_del()
    {
        M('auth_group_home')->where(array('id'=>I('id')))->delete();
        $this->redirect('admin_group');           
    }



    
    public function home_group_edit()
    {  
        
        if (!IS_AJAX)
        {
            $group=M('auth_group_home')->where(array('id'=>I('id')))->find();
            $this->assign('group',$group);
            $this->display();      
        }
        else
        {
            $data=array(
                'id'=>I('id'),
                'title'=>I('title'),
                'status'=>I('status'),
            );

            M('auth_group_home')->save($data);
            $this->success('角色修改成功',U('home_group'),1);
        }
    }

  
    
    public function home_group_state()
    {

        $id=I('x');
        $status=M('auth_group_home')->where(array('id'=>$id))->getField('status');//判断当前状态情况
        if($status==1)
        {
            $statedata = array('status'=>0);
            $auth_group=M('auth_group_home')->where(array('id'=>$id))->setField($statedata);
            $this->success('状态禁止',1,1);
        }
        else
        {
            $statedata = array('status'=>1);
            $auth_group=M('auth_group_home')->where(array('id'=>$id))->setField($statedata);
            $this->success('状态开启',1,1);
        }
    }
    //前台用户组管理end



    
    public function admin_rule()
    {
    	$nav = new \Org\Util\Leftnav;
    	$admin_rule=M('auth_rule')->order('sort')->select();
    	$arr = $nav::rule($admin_rule);
	   	$this->assign('admin_rule',$arr);//权限列表
    	$this->display();
    }


    
    public function admin_rule_add()
    {
    	if(!IS_AJAX)
        {
    		$this->error('提交方式不正确',0,0);
    	}
        else
        {
    		$admin_rule=M('auth_rule');
    		$data=array(
				'name'=>I('name'),
				'title'=>I('title'),
				'status'=>I('status'),
				'sort'=>I('sort'),
				'addtime'=>time(),
				'pid'=>I('pid'),
				'css'=>I('css'),
    		);
    		$admin_rule->add($data);
    		$this->success('菜单添加成功',U('admin_rule'),1);
    	}
    }



    
    public function admin_rule_state()
    {
    	$id=I('x');
    	$statusone=M('auth_rule')->where(array('id'=>$id))->getField('status');//判断当前状态情况
    	if($statusone==1)
        {
    		$statedata = array('status'=>0);
    		$auth_group=M('auth_rule')->where(array('id'=>$id))->setField($statedata);
    		$this->success('状态禁止',1,1);
    	}
        else
        {
    		$statedata = array('status'=>1);
    		$auth_group=M('auth_rule')->where(array('id'=>$id))->setField($statedata);
    		$this->success('状态开启',1,1);
    	}
    
    }



    
    public function ruleorder()
    {
    	if (!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		$auth_rule=M('auth_rule');
    		foreach ($_POST as $id => $sort){
    			$auth_rule->where(array('id' => $id ))->setField('sort' , $sort);
    		}
    		$this->success('排序更新成功',U('admin_rule'),1);
    	}
    }
    



    public function admin_rule_edit(){
     
        if(!IS_AJAX){
            $admin_rule=M('auth_rule')->where(array('id'=>I('id')))->find();
            $this->assign('rule',$admin_rule);
            $this->display();
    	}else{
    		$admin_rule=M('auth_rule');
    		$data=array(
				'id'=>I('id'),
				'name'=>I('name'),
				'title'=>I('title'),
				'status'=>I('status'),
				'css'=>I('css'),
				'sort'=>I('sort'),
    		);
    		$admin_rule->save($data);
    		$this->success('菜单修改成功',U('admin_rule'),1);
    	}
    }

   
    
    public function admin_rule_del()
    {
    	M('auth_rule')->where(array('id'=>I('id')))->delete();
    	$this->redirect('admin_rule');
    }


    
        //前台菜单



         public function nav_home()
    {
        $nav = new \Org\Util\Leftnav;
        $home_nav=M('auth_rule_home')->order('sort')->select();
        $arr = $nav::rule($home_nav);
        $this->assign('home_nav',$arr);//权限列表
        $this->display();
    }


    
    public function nav_home_add()
    {
        if(!IS_AJAX)
        {
            $this->error('提交方式不正确',0,0);
        }
        else
        {
            $admin_rule=M('auth_rule_home');
            $data=array(
                'name'=>I('name'),
                'title'=>I('title'),
                'status'=>I('status'),
                'sort'=>I('sort'),
                'addtime'=>time(),
                'pid'=>I('pid'),
                'css'=>I('css'),
            );
            $admin_rule->add($data);
            $this->success('菜单添加成功',U('nav_home'),1);
        }
    }



    
    public function nav_home_state()
    {
        $id=I('x');
        $statusone=M('auth_rule_home')->where(array('id'=>$id))->getField('status');//判断当前状态情况
        if($statusone==1)
        {
            $statedata = array('status'=>0);
            $auth_group=M('auth_rule_home')->where(array('id'=>$id))->setField($statedata);
            $this->success('状态禁止',1,1);
        }
        else
        {
            $statedata = array('status'=>1);
            $auth_group=M('auth_rule_home')->where(array('id'=>$id))->setField($statedata);
            $this->success('状态开启',1,1);
        }
    
    }



    
    public function home_ruleorder()
    {
        if (!IS_AJAX){
            $this->error('提交方式不正确',0,0);
        }else{
            $auth_rule=M('auth_rule');
            foreach ($_POST as $id => $sort){
                $auth_rule->where(array('id' => $id ))->setField('sort' , $sort);
            }
            $this->success('排序更新成功',U('admin_rule'),1);
        }
    }
    



    public function nav_home_edit(){
     
        if(!IS_AJAX){
            $admin_rule=M('auth_rule_home')->where(array('id'=>I('id')))->find();
            $this->assign('rule',$admin_rule);
            $this->display();
        }else{
            $admin_rule=M('auth_rule_home');
            $data=array(
                'id'=>I('id'),
                'name'=>I('name'),
                'title'=>I('title'),
                'status'=>I('status'),
                'css'=>I('css'),
                'sort'=>I('sort'),
            );
            $admin_rule->save($data);
            $this->success('菜单修改成功',U('nav_home'),1);
        }
    }

   
    
    public function nav_home_del()
    {
        M('auth_rule_home')->where(array('id'=>I('id')))->delete();
        $this->redirect('nav_home');
    }



    //三重权限配置
    public function admin_group_access(){
        
        if(!IS_POST){
    		$admin_group=M('auth_group')->where(array('id'=>I('id')))->find();
            $m = M('auth_rule');
            $data = $m->field('id,name,title,pid')->select();
            //p($data);die;
            $this->assign('admin_group',$admin_group);	// 顶级
            $this->assign('datas',$data);	// 顶级
            $this->display();
    	}else{
                  
            $m = M('auth_group');
            $new_rules = I('new_rules');
            $imp_rules = implode(',', $new_rules).',';
            $data=array(
                'id'=>I('id'),
                'rules'=>$imp_rules,	
            );
            if($m->save($data)){
                $this->success('权限配置成功',U('admin_group'),1);
            }else{
                $this->error('权限配置失败',U('admin_group'),1);
            }        
        
        }       
    
    }

     //前台三重权限配置
    public function home_group_access(){
        
        if(!IS_POST){
            $admin_group=M('auth_group_home')->where(array('id'=>I('id')))->find();
            $m = M('auth_rule_home');
            $data = $m->field('id,name,title,pid')->select();
            //p($data);die;
            $this->assign('admin_group',$admin_group);  // 顶级
            $this->assign('datas',$data);   // 顶级
            $this->display();
        }else{
                  
            $m = M('auth_group_home');
            $new_rules = I('new_rules');
            $imp_rules = implode(',', $new_rules).',';
            $data=array(
                'id'=>I('id'),
                'rules'=>$imp_rules,    
            );
            if($m->save($data)){
                $this->success('权限配置成功',U('home_group'),1);
            }else{
                $this->error('权限配置失败',U('home_group'),1);
            }        
        
        }       
    
    }
 

    //修改密码
    public function pwd() {

        $admin = M('admin');

        if (IS_AJAX) {

            $data['admin_username'] = I('post.admin_username');
            $data['admin_pwd'] = md5(I('post.oldpassword'));
            $pwd['admin_pwd'] = md5(I('post.newpassword'));
            $repassword = md5(I('post.repassword'));                    
            $result = $admin -> where($data) -> find();

            if ($result) {

                if ($pwd['admin_pwd'] != $repassword) {

                    $this -> error("两次输入新密码不一致");

                } else {
                    
                   $msg = $admin -> where($data) -> save($pwd);
                                       
                    if ($msg) {

                        $this -> success("密码修改成功", U('Login/login'));
                        return;
                    } else {

                        $this -> error("修改失败");
                        return;
                    }
                }

            } else {

                $this -> error("两次输入新密码不一致");
            }
        }

        $this -> display();
    }
    //报单相关变量设置（修改价格等）
    public function  baodanset(  )
    {
       if (!IS_AJAX)
        {
            $sys=M('sys')->where(array('sys_id'=>1))->find();
            $this->assign('sys',$sys)->display();
        }
        else
        {   

            $_POST['jfzr_open']=$_POST['jfzr_open']?$_POST['jfzr_open']:0;
            M('sys')->save($_POST);
            $this->success('设置成功',1,1);
        }
    }



    
}